﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace GUI.KhoHang
{
    public partial class frmXuLy : Form
    {
        public frmXuLy()
        {
            InitializeComponent();
        }
        public frmXuLy(string hanhDong, Entities.KhoHang KhoHang)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.khohang = KhoHang;
        }
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

        #region Khởi tạo
        public bool kiemtra1 = false;
        bool checkClose = false;
      //  Entities.KhoHang khohang;
        Entities.KhoHang temp;
        private string MaKhoHang = string.Empty;
        DateTime date;
        string hanhdong;
        string checkMaThuKho = "";
        string TenThuKho = "";
        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private Entities.KhoHang khohang;

        public Entities.KhoHang Khohang
        {
            get { return khohang; }
            set { khohang = value; }
        }
        #endregion 

        #region check validate

        public bool Validate()
        {
            try
            {
                if (txtmakho.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbmakho.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtmakho.Focus();
                    return false;
                }
                if (txttenkho.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbtenkho.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txttenkho.Focus();
                    return false;
                }
                if (cbxthukho.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbthukho.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    cbxthukho.Focus();
                    return false;
                }

                if (txtdienthoai.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbldienthoai.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtdienthoai.Focus();
                    return false;
                }

                if (Common.ValidateControl.IsNumber(txtdienthoai.Text) == false)
                {
                    MessageBox.Show(Common.MSG.MSG14(lbldienthoai.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtdienthoai.Text = "";
                    txtdienthoai.Focus();
                    return false;
                }

                return true;
            }
            catch
            {
                return true;

            }

        }
        #endregion

        #region lấy ID
        // xử lý ID
        private string GetID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "KhoHang";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "MK_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "MK_0001";
            }
            return code;
        }
        #endregion

        #region Sử Lý Dữ Liệu
        public void XuLyDuLieu()
        {
            try
            {
                txtmakho.Text = khohang.MaKho;
                txttenkho.Text = khohang.TenKho;
                cbxthukho.SelectedValue = khohang.MaNhanVien;
                txtdienthoai.Text = khohang.DienThoai;
                txtdiachi.Text = khohang.DiaChi;
                txtghichu.Text = khohang.GhiChu;

            }
            catch
            {


            }
        }
        #endregion


        #region Xử lý đối tượng
        // Insert data of phiếu chi into database
        public Entities.KhoHang XuLyObject(string hanhdong)
        {
            try
            {
                if (Validate())
                {
                    Entities.KhoHang khohang = new Entities.KhoHang();

                    khohang.HanhDong = hanhdong;
                    khohang.MaKho = txtmakho.Text.ToString();
                    khohang.TenKho = txttenkho.Text.ToString();
                    khohang.DienThoai = txtdienthoai.Text.ToString();
                    khohang.DiaChi = txtdiachi.Text.ToString();
                    khohang.TenNhanVien = this.TenThuKho;
                    khohang.MaNhanVien = this.checkMaThuKho ;
                    khohang.GhiChu = txtghichu.Text.ToString();
                    khohang.Deleted = false;
                    khohang.TaiKhoan = UtilitiesVariable.User;

                    return khohang;
                }
                return null;
            }
            catch
            {
                return null;
            }

        }
        #endregion 

        #region thủ kho
        private Entities.GetCombobox[] thukho = null;
        private void HienThiThuKho(string mathukho)
        {
            try
            {
                for (int i = 0; i < thukho.Length; i++)
                {
                    if (thukho[i].TenCotKhoaChinh == mathukho)
                    {
                        cbxthukho.SelectedItem = i;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception)
            {
                cbxthukho.Items.Clear();
                checkMaThuKho = string.Empty;
                TenThuKho = string.Empty;
            }
        }
        private void ThuKho()
        {
            try
            {
                cbxthukho.Items.Clear();
                Entities.GetCombobox pb = new Entities.GetCombobox();
                pb.TenBang = "NhanVien";
                pb.TenCotKhoaChinh = "MaNhanVien";
                pb.TenCotTenHienThi = "TenNhanVien";
                thukho = Common.StaticVariable.InterfaceObject.SelectCombox(pb);
                Common.Utilities.XuLyComboBox(cbxthukho, thukho, "TenCotTenHienThi", "TenCotKhoaChinh");
            }
            catch (Exception ex)
            {
                cbxthukho.Items.Clear();
                string s = ex.Message;
            }
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            try
            {
                ThuKho();
                date = Common.StaticVariable.InterfaceObject.SelectDate();
                switch (this.hanhDong)
                {
                    case "Them":
                       txtmakho.Text  = this.GetID();
                        break;
                    case "Sua":
                        XuLyDuLieu();
                        break;
                    default:
                        this.Thoat();
                        break;
                }
                this.WindowState = FormWindowState.Normal;
                txttenkho.Focus();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Quyen
        private Boolean Quyen()
        {
            return true;
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", "Thông báo");
                    return;
                }
                Entities.KhoHang nv = this.XuLyObject("Insert");
                txtmakho.Text = this.GetID();
                if (nv.MaKho != txtmakho.Text)
                {
                    nv = this.XuLyObject("Insert");
                }
                if (this.Validate() == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertKhoHang(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion
        private Entities.KhoHang[] ListKhoHang;
        private void GetRow()
        {
            try
            {
                if (ListKhoHang == null) { return; }
                for (int i = 0; i < this.ListKhoHang.Length; i++)
                {
                    if (ListKhoHang[i].MaKho == this.MaKhoHang)
                    {
                        temp = ListKhoHang[i];
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #region Sua
        private void Sua()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền sửa", "Thông báo");
                    return;
                }
                Entities.KhoHang nv = this.XuLyObject("Update");
                if (this.Validate() == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.UpdateKhoHang(nv);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = false;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        public bool CheckThuKho()
        {
            try
            {
                Entities.KhoHang nv=new Entities.KhoHang();
                nv.HanhDong="1";
                ListKhoHang = Common.StaticVariable.InterfaceObject.SelectKhoHang(nv);
                for (int i = 0; i < ListKhoHang.Length; i++)
                {
                    if (cbxthukho.SelectedValue.ToString() == ListKhoHang[i].MaNhanVien.ToString() && ListKhoHang[i].Deleted == false)
                    {
                        MessageBox.Show("Thủ kho '" + cbxthukho.Text + "' đã tồn tại");
                        cbxthukho.Focus();
                        return true;
                    }

                }
                return false;
            }
            catch
            {
                return false;

            }

        }

        public void CheckThuKhoKhi_Update()
        {
            try
            {
                Entities.KhoHang nv=new Entities.KhoHang();
                nv.HanhDong="1";
                ListKhoHang = Common.StaticVariable.InterfaceObject.SelectKhoHang(nv);
                for (int i = 0; i < ListKhoHang.Length; i++)
                {
                    if (cbxthukho.SelectedValue.ToString() == ListKhoHang[i].MaNhanVien.ToString() && ListKhoHang[i].Deleted == false)
                    {
                        MessageBox.Show("Thủ kho '" + cbxthukho.Text + "' đã tồn tại");
                        cbxthukho.Focus();
                        return;
                    }
                    Sua();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region envent
        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
        private void txtdienthoai_TextChanged(object sender, EventArgs e)
        {
            try
            {
                TextBox temp = (TextBox)sender;
                int vt = temp.SelectionStart;
                string str = new string(temp.Text.ToList<char>().Where(c => char.IsNumber(c)).ToArray<char>());
                temp.Text = str;
                temp.SelectionStart = vt;
            }
            catch
            {
            }
        }
        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.hanhDong)
                {
                    case "Them":
                        //if (CheckThuKho() == false)
                        //{
                            this.Them();
                       // }
                        break;
                    case "Sua":
                      //  if (checkMaThuKho == cbxthukho.SelectedValue.ToString())
                      //  {
                            this.Sua();
                       // }
                        //else
                        //{
                        //    CheckThuKhoKhi_Update();
                        //}
                        break;
                }
            }
            catch (Exception)
            {

            }
        }
       
        private void cbxthukho_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Entities.GetCombobox row = (Entities.GetCombobox)cbxthukho.SelectedItem;
                this.checkMaThuKho = row.TenCotKhoaChinh;
                this.TenThuKho = row.TenCotTenHienThi;
            }
            catch (Exception)
            {
                this.checkMaThuKho = string.Empty;
                this.TenThuKho = string.Empty;
            }
        }
        #endregion
    }
}
